//
//  MyCollectMainVC.m
//  ReceiptDelivery
//
//  Created by 吕晶 on 2020/10/28.
//  Copyright © 2020 吕晶. All rights reserved.
//

#import "MyCollectMainVC.h"
#import "WaybillMainModel.h"
#import "MyCollectListCell.h"
#import "LadingBillNavBar.h"
#import "MyCollectDetailVC.h"
#import "WoShouQianShouVC.h"
#import "WayBillGuiJiVC.h"

@interface MyCollectMainVC ()<UITableViewDelegate,UITableViewDataSource>{
    int page;
    NSInteger listStatus;
}

@property (nonatomic, strong) LadingBillNavBar *navBar;
@property (nonatomic, strong) UITableView *tableView;
@property (nonatomic, strong) NSMutableArray <WaybillMainModel *>*array;

@end

@implementation MyCollectMainVC

-(LadingBillNavBar *)navBar {
    if (!_navBar) {
        _navBar = [[LadingBillNavBar alloc] initWithFrame:CGRectMake(0, 0, ScreenW, NaviHeight)];
        _navBar.titleStr = @"我收的";
        __weak_self;
        _navBar.backBlock = ^{
            [weakself.rt_navigationController popViewControllerAnimated:YES];
        };
        _navBar.changeBlock = ^(NSInteger status) {
            [weakself changeView:status];
        };
    }
    return _navBar;
}

- (void)changeView:(NSInteger)status {
    page = 1;
    listStatus = status;
    [self requestForList:listStatus];
}

-(UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, self.navBar.nim_bottom, ScreenW, ScreenH - BottomHeight - NaviHeight) style:UITableViewStyleGrouped];
        _tableView.backgroundColor = Color_backgroundColor;
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        __weak_self;
        _tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page = 1;
            [strongSelf requestForList:strongSelf->listStatus];
        }];
        _tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
            __strong_self;
            strongSelf->page++;
            [strongSelf requestForList:strongSelf->listStatus];
        }];
        _tableView.ly_emptyView = [LYEmptyView emptyViewWithImageStr:@"img_occupy"
             titleStr:@""
            detailStr:@""];
    }
    return _tableView;
}

-(NSMutableArray<WaybillMainModel *> *)array {
    if (!_array) {
        _array = [NSMutableArray array];
    }
    return _array;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBar.hidden = YES;
    page = 1;
    listStatus = 1;
    [self.view addSubview:self.navBar];
    [self.view addSubview:self.tableView];
    [self requestForList:listStatus];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(refreshList) name:ReceiptDeliveryRefreshWoShouList object:nil];
}

- (void)refreshList {
    page = 1;
    [self requestForList:listStatus];
}

- (void)requestForList:(NSInteger)status {
    NSMutableDictionary *dic = [NSMutableDictionary dictionary];
    [dic setObject:@(status) forKey:@"type"];
    [dic setObject:@(page) forKey:@"page"];
    [HttpRequest PostWithApi:ReceiptDeliveryMyCollectList params:dic progress:^(NSProgress * _Nonnull progress) {

    } success:^(NSURLSessionDataTask * _Nonnull operation, id  _Nonnull responseObject, BOOL code, NSInteger errorCode) {
        if (code) {
            if (self->page == 1) {
                [self.array removeAllObjects];
                self.array = [WaybillMainModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
            }else {
                NSArray *array = [WaybillMainModel mj_objectArrayWithKeyValuesArray:responseObject[@"list"]];
                [self.array addObjectsFromArray:array];
            }
            BOOL has_more = [responseObject[@"has_more"] boolValue];
            if (has_more) {
                self.tableView.mj_footer.hidden = NO;
            }else {
                self.tableView.mj_footer.hidden = YES;
            }
            [self.tableView reloadData];
        }
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    } fail:^(NSError * _Nonnull error) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
    }];
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.array.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectListCell *cell = [MyCollectListCell MyCollectListCellWithTableView:tableView];
    WaybillMainModel *model = self.array[indexPath.row];
    cell.model = model;
    __weak_self;
    cell.detailBlock = ^{
        MyCollectDetailVC *vc = [MyCollectDetailVC new];
        vc.wa_id = model.wa_id;
        [weakself.rt_navigationController pushViewController:vc animated:YES];
    };
    cell.guiJiBlock = ^{
        WayBillGuiJiVC *vc = [WayBillGuiJiVC new];
        vc.wa_id = model.wa_id;
        vc.type = @"2";
        [weakself.rt_navigationController pushViewController:vc animated:YES];
    };
    cell.qianShouBlock = ^{
        WoShouQianShouVC *vc = [WoShouQianShouVC new];
        vc.wa_id = model.wa_id;
        [weakself.rt_navigationController pushViewController:vc animated:YES];
    };
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    MyCollectListCell *cell = [MyCollectListCell MyCollectListCellWithTableView:tableView];
    cell.model = self.array[indexPath.row];
    return cell.height;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    WaybillMainModel *model = self.array[indexPath.row];
    MyCollectDetailVC *vc = [MyCollectDetailVC new];
    vc.wa_id = model.wa_id;
    [self.rt_navigationController pushViewController:vc animated:YES];
}

-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return [UIView new];
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return 0;
}

-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section {
    UIView *view = [[UIView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, 10)];
    view.backgroundColor = Color_backgroundColor;
    return view;
}

-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section {
    return 10;
}

-(void)dealloc {
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}

@end
